#!/usr/bin/env python
# encoding: utf-8
"""
@summary: 计算2014年7月 周活跃用户; 计算7日内两次登录的用户数;在DAU.py的基础上改编
@author: hongxingfan
@since: 2014年8月20日    上午10:28:44
"""
import sys
import os

# 7月1号-7月7号-->在7日内suv出现2次及以上的
# 7月7号-7月13号

if __name__ == "__main__":
    filePath = sys.argv[1]
    result_hash = {}
    day_arr = ("20140701", "20140702", "20140703", "20140704", "20140705", "20140706", "20140707")
    # day_arr = ("20140707", "20140708", "20140709", "20140710", "20140711", "20140712", "20140713")
    num = 0
    
    if os.path.exists(filePath):
        handler = open(filePath, "r")
        for line in handler:
            num += 1
            if num % 500000 == 0:
                print("已处理 %d 条记录" % (num))
            
            line = line.strip()
            cols = line.split("\t")
            suv = cols[0]
            length = len(cols[1])
            values = cols[1][1:length - 2]  # 子串 如：2014-07-08:6;2014-07-17:31
            values_1 = values.split(";")  # 2014-07-08:6  2014-07-17:31
            for val in values_1:
                index = val.index(":")
                day = val[0:index]  # 2014-07-08
                if day not in day_arr:
                    continue
                if suv in result_hash:
                    result_hash[suv] += 1
                else:
                    result_hash[suv] = 1
    else:
        sys.stderr.write("'%s' is an error path!" % (filePath))
        sys.exit(0)
    
    # 输出
    wau = 0
    for (v) in result_hash.values():
        if v > 1:
            wau += 1
    print("周活跃用户数 %d" % (wau))
